package com.green.web.util;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.green.model.manage.Person;
import com.green.service.common.ServiceContext;

/**
 * 上下文
 * 
 * @author fengwenjin
 * 
 */
public class WebContext {

	private static ThreadLocal<HttpServletRequest> requestLocal = new ThreadLocal<HttpServletRequest>();
	private static ThreadLocal<HttpServletResponse> responseLocal = new ThreadLocal<HttpServletResponse>();
	private static ServletContext servletContext;

	/**
	 * 初始化
	 * 
	 * @param context
	 */
	public static void init(ServletContext context) {
		servletContext = context;
	}

	/**
	 * 获取请求对象
	 * 
	 * @return HttpServletRequest
	 */
	public static HttpServletRequest getRequest() {
		return requestLocal.get();
	}

	/**
	 * 设置请求对象
	 * 
	 * @param request
	 */
	public static void setRequest(HttpServletRequest request) {
		requestLocal.set(request);
	}

	/**
	 * 获取响应对象
	 * 
	 * @return HttpServletResponse
	 */
	public static HttpServletResponse getResponse() {
		return responseLocal.get();
	}

	/**
	 * 设置响应对象
	 * 
	 * @param response
	 */
	public static void setResponse(HttpServletResponse response) {
		responseLocal.set(response);
	}

	/**
	 * 获取ServletContext
	 * 
	 * @return ServletContext
	 */
	public static ServletContext getServletContext() {
		return servletContext;
	}
	
	/**
	 * 设置当前城市
	 * 
	 * @param city
	 */
	public static void setCurrentCity(String city){
		Person person = ServiceContext.getLoginPerson();
		person.setCurrentCity(city);
		SessionHelper.setAttribute(Constants.CURRENT_PERSON, person);
	}
	
	/**
	 * 获取当前城市
	 * 
	 * @param city
	 */
	public static String getCurrentCity(){
		return ServiceContext.getLoginPerson().getCurrentCity();
	}
	
}
